﻿using SanXiao.Game.Common;
using SanXiao.Game.GameCenter;
using SanXiao.Game.GameCenter.Bubbles;
using SanXiao.Game.GameCenter.Roles;
using SanXiao.Game.View.ViewEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SanXiao.Game.View
{
    public partial class FrmStart : Form
    {
        private readonly ImageObject _back;
        private readonly ImageObject _login;
        private readonly RoleObject _role;
        private readonly System.Timers.Timer _timer;
        public FrmStart()
        {
            InitializeComponent();
            SetStyle(
          ControlStyles.ResizeRedraw
          | ControlStyles.DoubleBuffer
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.SupportsTransparentBackColor,
          true
          );

            _back = new ImageObject(Properties.Resources.start);
            _login = new ImageObject(Properties.Resources.login);
            _role = RoleCenter.CreateRole();
            _role.Width = 332;
            _role.Height = 450;
            _role.X = 50;
            _role.Y = 50;

            this.Paint += FrmStart_Paint;
            this.FormClosing += FrmStart_FormClosing;
            this.SizeChanged += FrmStart_SizeChanged;
            this.Load += FrmStart_Load;
            this.MouseDown += FrmStart_MouseDown;

            _timer = new System.Timers.Timer();
            _timer.Interval = 100;
            _timer.Elapsed += _timer_Elapsed;
            _timer.Start();
        }

        private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invalidate();
        }

        private void FrmStart_MouseDown(object? sender, MouseEventArgs e)
        {
            if (!clickableArea.Contains(e.Location)) return;

            FrmMain main = new FrmMain();
            this.Hide();
            main.ShowDialog();
            main.Dispose();
            this.Show();
        }

        private void FrmStart_Load(object? sender, EventArgs e)
        {
            UpdateClickableArea();
        }

        private void FrmStart_SizeChanged(object? sender, EventArgs e)
        {
            UpdateClickableArea();
            this.Invalidate();
        }

        private void FrmStart_FormClosing(object? sender, FormClosingEventArgs e)
        {
            _timer.Stop();
            _timer.Dispose();
            _back.Dispose();
            _role.Dispose();
            _login.Dispose();
            this.Dispose();
        }
        private void FrmStart_Paint(object? sender, PaintEventArgs e)
        {
            IntPtr hdc = e.Graphics.GetHdc();
            GDI32.DrawImage2(hdc, _back.BitmapPtr, 0, 0, _back.Width, _back.Height, this.Width, this.Height);
            GDI32.DrawImageTransparenc(hdc, _login.BitmapPtr, clickableArea.X, clickableArea.Y, _login.Width, _login.Height, clickableArea.Width, clickableArea.Height);
            e.Graphics.ReleaseHdc(hdc);
            _role.Draw(e.Graphics);
        }
        private Rectangle clickableArea;
        private void UpdateClickableArea()
        {
            float zoomX = (float)this.Width / 1076;
            float zoomY = (float)this.Height / 605;
            int areaWidth = (int)(180 * zoomX);
            int areaHeight = (int)(50 * zoomY);
            int areaX = this.Width - areaWidth >> 1;
            int areaY = (int)(475 * zoomY);

            clickableArea = new Rectangle(areaX, areaY, areaWidth, areaHeight);
            _role.SizeChange(zoomX, zoomY);
        }
    }
}
